/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-01-24
 * Time: 21:48
 */
public class Sort {
    // 选择排序
    public  void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int tmp=i;
            for (int j = i+1; j <array.length; j++) {
                if(array[j]<array[tmp]){
                    tmp=j;
                }
            }
            swap(array,i,tmp);
        }

    }
    private void swap(int[] array,int i,int tmp){
        int k=array[i];
        array[i]=array[tmp];
        array[tmp]=k;

    }


    public void selectSort2(int[] array){
        int left=0;
        int right=array.length-1;
        while(left<right){
            int minIndex=left;
            int maxIndex=left;
            int i = left+1;
            for (; i <=right ; i++) {
                if(array[i]<array[minIndex]){
                    minIndex=i;
                }
                if(array[i]>array[maxIndex]){
                    maxIndex=i;
                }
            }
            swap(array,left,minIndex);
            if(maxIndex==left){
                maxIndex=minIndex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }

}
